<template>
  <lc-sf moda="edit" ref="sf" :schema="xx" />
  <div class="dialog-footer">
    <el-button @click="helper.close()">取消</el-button>
    <el-button type="primary" @click="updateWord" :disabled="!vaild"
      >修改密码</el-button
    >
  </div>
</template>
<script setup lang="ts" name="update-password">
import { Dialog } from "@/components/model";
import LcSf from "@/components/sf/sf.vue";
import { InputUI, SFSchema } from "@/components/sf/interface";

import { Subject } from "rxjs";
import { NzSafeAny, SFInstance } from "@/components/sf/type";
import { computed, ref } from "vue";
import { Http } from "@/core/api/http.service";
import { ElNotification } from "element-plus";
import { useUserHooks } from "@/core/pinia/store/user.store";
const user = useUserHooks();
const props = defineProps<{
  helper: Dialog;
  openSub: Subject<null>;
  i: Record<string, NzSafeAny>;
}>();
const xx = {
  properties: {
    password: {
      title: "原始密码",
      ui: {
        placeholder: "原始密码",
        showPassword: true,
        clearable: true
      } as InputUI
    },
    newPassword: {
      title: "修改密码",
      ui: {
        placeholder: "最新密码",
        showPassword: true,
        vaildPassword: true,
        clearable: true
      }
    },
    okPassword: {
      title: "确认密码",
      ui: {
        placeholder: "确认密码",
        showPassword: true,
        clearable: true
      }
    }
  },
  required: ["password", "okPassword", "newPassword"]
} as SFSchema<unknown>;
const sf = ref<SFInstance>();
const vaild = computed(() => {
  if (!sf.value?.service) {
    return true;
  }
  return sf.value?.service.vaild;
});
props.openSub.subscribe(r => {
  console.log("监听呢---》", r);
});

const updateWord = async () => {
  await Http.of().post("/login/update/password", sf.value.service.getValues());
  ElNotification({
    title: "温馨提示",
    message: "密码修改成功，即将退到登录页面",
    type: "info",
    duration: 2000,
    showClose: false
  });
  await new Promise(r => setTimeout(() => r(null), 1000));
  user.logout();
  props.helper.close(true);
};
</script>
@/components/sf/interfaces @/components/sf/types
